package com.yuhong.springbootinit.job.cycle;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuhong.springbootinit.mapper.ApplicationMapper;
import com.yuhong.springbootinit.mapper.InterviewMapper;
import com.yuhong.springbootinit.model.entity.Application;
import com.yuhong.springbootinit.model.entity.Interview;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: resume-analysis-system
 * @BelongsPackage: com.yuhong.springbootinit.job.cycle
 * @Author: renyuhong
 * @CreateTime: 2025-04-24  16:27
 * @Description:
 * @Version: 1.0
 */
@Component
@Slf4j
public class ApplicationToInterviewTask {

    @Resource
    private ApplicationMapper applicationMapper;

    @Resource
    private InterviewMapper interviewMapper;

    /**
     * 每分钟执行一次：处理 status = 2 的申请，生成面试记录
     */
    @Scheduled(fixedRate =60 * 1000)
    public void generateInterviewFromApplication() {
        // 查询 status = 2 的 application
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 2);
        List<Application> applications = applicationMapper.selectList(queryWrapper);

        if (CollUtil.isEmpty(applications)) {
            log.info("No applications with status=2 found.");
            return;
        }

        log.info("Start processing {} applications to generate interviews", applications.size());

        for (Application application : applications) {
            // 创建 Interview 对象
            Interview interview = new Interview();
            interview.setUserId(application.getUserId());
            interview.setCompanyId(application.getCompanyId());
            interview.setPositionId(application.getPositionId());
            interview.setCompanyName(application.getCompanyName());
            interview.setDescription("系统根据投递生成的面试");
            interview.setStatus(1); // 初始状态为 0（未开始）

            interviewMapper.insert(interview);

            // 更新 application 状态为 4
            application.setStatus(4);
            applicationMapper.updateById(application);
        }

        log.info("Finished generating interviews from applications.");
    }


}
